package cn.bluemobi.platform.controller.member;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.controller.PlatformBaseController;
import cn.bluemobi.platform.core.dto.DatatablePage;
import cn.bluemobi.platform.entity.member.Identification;
import cn.bluemobi.platform.entity.member.Member;
import cn.bluemobi.platform.service.MemberService;

/**
 * 用户管理controller
 * 
 * @author qifnagke
 * 
 */
@Controller
@RequestMapping("/admin/member")
public class MemberController extends PlatformBaseController {

	@Autowired
	private MemberService memberService;

	@RequestMapping("/toMemberPage")
	public String toMemberPage(ModelMap model) {
		model.put("totalMemberWaitRebatePoints",
				memberService.getTotalMemberWaitRebatePoints());
		model.put("totalMemberAlreadyRebatePoints",
				memberService.getTotalMemberAlreadyRebatePoints());
		model.put("totalTodayRebateGoldPoints",
				memberService.getTotalTodayRebateGoldPoints());
		model.put("totalTodayRebateSilverPoints",
				memberService.getTotalTodayRebateSilverPoints());
		return "member/memberList";
	}

	@RequestMapping("/findMembers")
	@ResponseBody
	public DatatablePage findMembers(PageCondition cond) {
		return new DatatablePage(cond, memberService.findMembers(cond));
	}

	@RequestMapping("/toMemberDetailPage")
	public String toMemberDetailPage(
			@RequestParam(value = "memberId", required = false) String memberId,
			ModelMap model) {
		if (!StringUtils.isEmpty(memberId)) {
			Member member = memberService.findMemberDetail(memberId);
			model.put("member", member);
			Identification i = memberService.findMemberIndentify(memberId);
			if (i != null) {
				model.put("indentify", i);
			}
		}
		return "member/memberDetail";
	}

	@RequestMapping("/identifyMember")
	@ResponseBody
	public MapDto identifyMember(@RequestParam("memberId") String memberId,
			@RequestParam("indentifyId") String indentifyId) {
		return memberService.disableMember(memberId, indentifyId, getUser(), 2);
	}

	@RequestMapping("/disableMember")
	@ResponseBody
	public MapDto disableMember(@RequestParam("memberId") String memberId) {
		return memberService.disableMember(memberId, null, getUser(), 1);
	}

	@RequestMapping("/enableMember")
	@ResponseBody
	public MapDto enableMember(@RequestParam("memberId") String memberId) {
		return memberService.enableMember(memberId, null, getUser());
	}

	@RequestMapping("/moditifyMember")
	@ResponseBody
	public MapDto moditifyMember(@RequestParam("memberId") String memberId,
								 @RequestParam("waitMemberRebatePoints") Double waitMemberRebatePoints,
								 @RequestParam("consumePoints") Double consumePoints,
								 @RequestParam("withdrawalsPoints") Double withdrawalsPoints
								 ) {
		return memberService.moditifyMember(memberId,waitMemberRebatePoints,consumePoints,withdrawalsPoints,getUser());
	}

}
